extends Control

#@onready var treeWidget = %Tree
@onready var block_tree = %block_tree

#弹出窗口
@onready var popTree = %popupTree
@onready var ability_selection_panel: PanelContainer = %ability_selection_panel


# 添加技能里面的几个状态及按钮
@onready var selectParentBtn = %selectParentBtn
@onready var nameLineEdit = %nameEdit
@onready var desTextEdit = %desEdit
@onready var colorPickBtn:ColorPickerButton =  %colorPicker

# 添加技能里面的几个状态及按钮
@onready var selectParentBtnInD = %selectParentBtnInD
@onready var nameLineEditInD = %nameEditinD
@onready var desTextEditInD = %desEditInD
@onready var colorPickBtnInD:ColorPickerButton =  %colorPickerInd

# 技能添加成功或者失败的信号
@onready var msgLabel = %msg_label

const  ABILITY_ADDED_ERROR_NO_NAME = 1
const  ABILITY_ADDED_ERROR_NO_DES = 2
const  ABILITY_ADDED_VALIDATED = 3


# 定义一个添加技能页面是否显示的状态
@onready var is_adding_ability_mode = true
# 两个面板
@onready var add_ability_container = %add_ability_container
@onready var ability_detail_container = %ability_detail_container


signal add_ability_mode_changed(isAdding:bool)

func _ready():
	#recontruct_tree(treeWidget)	
	Signalbus.ability_added.connect(_on_ability_changed)
	Signalbus.ability_deled.connect(_on_ability_changed)
	Signalbus.ability_added_error.connect(_on_ability_added_error)
	block_tree.node_selected.connect(_on_ability_node_selected)
	
	add_ability_mode_changed.connect(_on_add_mode_changed)
	
# 创建树
func recontruct_tree(tree:Tree):
	tree.clear()
	var root = tree.create_item()
	for ability_id in GlobalVariables.get_all_ability_keys():
		var ability_data = GlobalVariables.get_ability(ability_id)
		if ability_data.parent_id =='NULL':
			create_tree(root,ability_data)

#遍历整个树，创建树
func create_tree(node:TreeItem,ability_data:AbilityEntity):
	var child = node.create_child()
	child.set_text(0,ability_data.name)
	child.set_metadata(0,ability_data)
	child.set_tooltip_text(0,ability_data.description)
	if len(ability_data.children_id)==0:
		return
	else:
		for subchild_id in ability_data.children_id:
			var subchild_data = GlobalVariables.get_ability(subchild_id)
			create_tree(child,subchild_data)
	
#当选择父类
func _on_select_parent_btn_pressed():
	if not ability_selection_panel.visible:
		recontruct_tree(popTree)
		ability_selection_panel.show()

# 弹出框选择的时候
func _on_select_btn_pressed():
	#获得当前选择的treeItem
	var selected_item:TreeItem = popTree.get_selected()
	if selected_item==null:
		return 
	# 显示出技能名称
	var ability_name = selected_item.get_text(0)
	# 获得对应的ability_id
	var ability_data = selected_item.get_metadata(0)
	if is_adding_ability_mode:
		selectParentBtn.text = ability_name
		selectParentBtn.set_meta('parent_id',ability_data.id)
		colorPickBtn.color = ability_data.color
	else:
		selectParentBtnInD.text = ability_name
		selectParentBtnInD.set_meta('parent_id',ability_data.id)
		colorPickBtnInD.color = ability_data.color
		
	ability_selection_panel.hide()

func _on_cancel_btn_pressed():
	ability_selection_panel.hide()

func _on_clear_pressed():
	clear_ability_data()
		
func clear_ability_data():
	if is_adding_ability_mode:
		nameLineEdit.clear()
		desTextEdit.clear()
		selectParentBtn.set_text('选择父类')
		selectParentBtn.remove_meta('parent_id')
		colorPickBtn.set_pick_color(Color('#115740'))
	else:
		nameLineEditInD.clear()
		desTextEditInD.clear()
		selectParentBtnInD.set_text('选择父类')
		selectParentBtnInD.remove_meta('parent_id')
		colorPickBtnInD.set_pick_color(Color('#115740'))
	
	msgLabel.hide()

# 技能详情页面中删除按钮
func _on_del_pressed():
	var ability_data  = get_ability_data(nameLineEditInD,desTextEditInD,selectParentBtnInD,colorPickBtnInD,false)
	# 首先是删除存储的数据
	GlobalVariables.del_ability(ability_data)
	GlobalVariables.save_ability_file()
	# 更新当前UI显示
	Signalbus.ability_deled.emit(ability_data)

# 当技能详情页面中更新按钮点击之后
func _on_update_pressed():
	var ability_data  = get_ability_data(nameLineEditInD,desTextEditInD,selectParentBtnInD,colorPickBtnInD,false)
	save_ability_data(ability_data)

#添加按钮之后
func _on_add_pressed():
	var ability_data = get_ability_data(nameLineEdit,desTextEdit,selectParentBtn,colorPickBtn)
	save_ability_data(ability_data)
		
func save_ability_data(ability_data:AbilityEntity):
	var validate_result = validate(ability_data)
	if  validate_result == ABILITY_ADDED_VALIDATED:
		save_ability(ability_data)
		# 发送技能填加成功的信号
		Signalbus.ability_added.emit(ability_data)
	else:
		var error_msg = '添加失败：'
		if validate_result == ABILITY_ADDED_ERROR_NO_NAME:
			error_msg+='技能名不能为空'
		elif validate_result == ABILITY_ADDED_ERROR_NO_DES:
			error_msg+= '技能描述不能为空'
		Signalbus.ability_added_error.emit(error_msg)
		
# 获取当前添加的技能数据
func get_ability_data(nameLE:LineEdit, desTE:TextEdit, selectBtn:Button, colorPBtn:ColorPickerButton, newID:bool=true)->AbilityEntity:
	var abEn = AbilityEntity.new()
	# 动态生成ID
	if newID:
		abEn.id = generate_ability_id()
	else:
		abEn.id = nameLE.get_meta('ability_id')
		pass
	abEn.name = nameLE.text
	abEn.description = desTE.text
	# parent 设置之后，需要从树形结构中获得对应的ability_entity的ID
	abEn.parent_id = selectBtn.get_meta('parent_id','NULL')
	abEn.color = colorPBtn.color
	return abEn

func generate_ability_id():
	return GlobalVariables.uuid_util.v4()

#保存技能
func save_ability(ability_data:AbilityEntity):
	#ConfigFile这个数据进行保存
	GlobalVariables.add_ability(ability_data)
	GlobalVariables.save_ability_file()
	
# 验证技能数据是否正确
func validate(ability_data:AbilityEntity):
	
	if ability_data.name=='':
		return ABILITY_ADDED_ERROR_NO_NAME
	elif ability_data.description == '':
		return ABILITY_ADDED_ERROR_NO_DES
	
	return ABILITY_ADDED_VALIDATED
	
	
## =====  技能改变的信号监听
func _on_ability_changed(_abEn:AbilityEntity):
	clear_ability_data()
	#recontruct_tree(treeWidget)
	block_tree.recontruct_tree()

func _on_ability_added_error(msg:String):
	msgLabel.text = msg
	msgLabel.show()

## 当技能节点被选中时的回调函数
func _on_ability_node_selected(node):
	var ability_data = node.abEn
	show_ability_detail(ability_data)

func show_ability_detail(ability_data:AbilityEntity):
	is_adding_ability_mode = false
	add_ability_mode_changed.emit(is_adding_ability_mode)
	if ability_data.parent_id == 'ROOT':
		return 
	else:
		update_ability_detail_widgets(ability_data)

func update_ability_detail_widgets(ability_data:AbilityEntity):
	nameLineEditInD.text = ability_data.name
	nameLineEditInD.set_meta('ability_id',ability_data.id)
	desTextEditInD.text = ability_data.description
	colorPickBtnInD.color = ability_data.color
	var parent_id = ability_data.parent_id
	if parent_id == 'NULL':
		selectParentBtnInD.text = '技能'
		selectParentBtnInD.add_theme_color_override("font_color",Color.DARK_RED)
	else:
		var parent_data = GlobalVariables.get_ability(parent_id)
		selectParentBtnInD.text = parent_data.name
		selectParentBtnInD.add_theme_color_override("font_color",parent_data.color)
	selectParentBtnInD.set_meta('parent_id',parent_id)
	

func _on_add_ability_btn_pressed():
	if not is_adding_ability_mode:
		is_adding_ability_mode = true
		add_ability_mode_changed.emit(is_adding_ability_mode)

func _on_add_mode_changed(isAdding:bool):
	add_ability_container.visible = isAdding
	ability_detail_container.visible = !isAdding


